<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addPer(0,1)">添加权限</el-button>
      <el-table :data="tableData" style="width: 100%" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作" prop="">
          <template v-slot="{row}">
            <!-- 如果type为1 就是一级权限  -->
            <el-button v-if="row.type===1" type="text" size="mini" @click="addPer(row.id,2)">添加</el-button>
            <el-button type="text" size="mini" @click="edit(row.id)">编辑</el-button>
            <el-button type="text" size="mini" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      title="新增权限"
      :visible.sync="showPer"
      width="30%"
      @close="hClose"
    >
      <el-form ref="perform" :model="perform" label-width="120px" :rules="rules">
        <el-form-item label="权限名称" style="width:90%" prop="name">
          <el-input v-model="perform.name" />
        </el-form-item>
        <el-form-item label="权限标识" style="width:90%" prop="code">
          <el-input v-model="perform.code" />
        </el-form-item>
        <el-form-item label="权限描述" style="width:90%">
          <el-input v-model="perform.description" type="textarea" />
        </el-form-item>
        <el-form-item label="开启" style="width:90%">
          <el-switch
            v-model="perform.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确定</el-button>
          <el-button @click="hClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getPerList, addPer, editPer, delPer, getPer } from '@/api/permission'
import { arr2tree } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      tableData: [],
      showPer: false,
      isEdit: '',
      perform: {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        type: '',
        pid: 1
      },
      rules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPerList()
  },
  methods: {
    // 删除
    async del(id) {
      await delPer(id)
      this.$message.success('删除成功')
      this.getPerList()
    },
    // 编辑
    async edit(id) {
      // console.log(id)
      this.showPer = true
      this.isEdit = id
      // 获取权限点详情
      const res = await getPer(id)
      // console.log(res)
      // 数据回显
      this.perform = res.data
      // this.$message.success('编辑权限成功')
    },
    // 添加子元素按钮
    async addPer(id, type) {
      this.showPer = true
      this.perform.type = type
      this.perform.pid = id
      // this.$message.success('添加权限成功')
    },
    // 点击确定
    async onSubmit() {
      this.$refs.perform.validate()
      // console.log(this.isEdit)
      if (this.isEdit) {
        // 修改请求
        await editPer(this.perform)
      } else {
        // 添加请求
        await addPer(this.perform)
      }
      // 确定之后要重新获取权限列表  清空表单 关闭按钮
      this.getPerList()
      // 调用取消按钮
      this.hClose()
    },

    // 获得权限列表
    async getPerList() {
      const res = await getPerList()
      // console.log(res)
      this.tableData = arr2tree(res.data, 0)
    },
    // 取消按钮
    hClose() {
      // 关闭弹框
      this.showPer = false
      // 清空表单
      this.$refs.perform.resetFields()
      // 因为description不是必填项 所以需要手动清空
      this.perform.description = ''
    }
  }
}
</script>
